import pandas as pd
import numpy as np

data = {
    "日期": pd.date_range("2024-01-01", periods=10, freq="D"),
    "产品": ["手机", "笔记本", "平板", "手机", "笔记本", "平板", "手机", "笔记本", "平板", "手机"],
    "销量": [100, 80, 60, 110, np.nan, 75, 95, 95, 70, 50],
    "单价": [3000, 5000, 2500, 3100, 4900, np.nan, 3050, 5200, 2600, 2950]
}

df = pd.DataFrame(data)

print("原始数据：\n", df)

print("\n数据概况：")
print(df.info())

# import numpy as np
#
# arr = np.array([1, 2, np.nan, 4, np.inf, -np.inf])
#
# # 检测
# print("NaN位置:", np.isnan(arr))
# print("无穷大位置:", np.isinf(arr))
#
# # 填充
# filled = np.nan_to_num(arr, nan=0, posinf=999, neginf=-999)
# print("填充后:", filled)
#
# # 过滤
# clean = arr[~np.isnan(arr)]
# print("过滤NaN后:", clean)
#
# # import numpy as np
# #
# # # 创建一个3x3的数组（比5x5小，方便观察）
# # data = np.array([[ 1.2, -0.5,0.8],[-1.1, 2.3, -0.2],[ 0.4, -0.9, 1.5]])
# #
# # threshold = 0.5
# # mask = data < threshold
# # data[data < threshold] = np.nan
# # print("替换后的数组 data:")
# # print(data)
#
# # mask = data > 0
# # positive_data = data[mask]
# # print("提取的正数 positive_data:")
# # print(positive_data)
#
# # import numpy as np
# #
# # # 创建 4x5 的二维数组
# # arr = np.array([[1, 2, 3, 4, 5],
# #                  [6, 7, 8, 9, 10],
# #                  [11,12,13,14,15],
# #                  [16,17,18,19,20]])
# #
# # # 选取所有行的第2-3列
# # result = arr[:, 1:3]
# # print("\narr[:, 1:3] - 所有行的第2-3列:")
# # print(result)
#
# # # 创建 4x5 的二维数组
# # arr = np.array([[1, 2, 3, 4, 5],
# #                 [6, 7, 8, 9, 10],
# #                 [11,12,13,14,15],
# #                 [16,17,18,19,20]])
# #
# # print("原始数组:")
# # print(arr)
# # print("形状:", arr.shape)  # (4, 5)
#
# # arr = np.array([10,20,30,40,50])
# # print(arr[[1,3,4]])  # 输出 [20 40 50]
#
# # # 1. 矩阵创建
# # A = np.array([[1, 2], [3, 4]])
# # B = np.array([[5, 6], [7, 8]])
# #
# # print("矩阵 A:\n", A)
# # print("矩阵 B:\n", B)
# #
# # # 2. 矩阵乘法
# # C = A @ B
# # print("\n矩阵乘法 A @ B:\n", C)
# #
# # # 3. 矩阵求逆
# # A_inv = np.linalg.inv(A)
# # print("\nA的逆矩阵:\n", A_inv)
# #
# # # 验证逆矩阵
# # identity = A @ A_inv
# # print("\n验证 A × A⁻¹:\n", identity)
# #
# # # 4. 特征值分解
# # eigenvalues, eigenvectors = np.linalg.eig(A)
# # print("\n特征值:", eigenvalues)
# # print("特征向量:\n", eigenvectors)
# #
# # # 验证特征值分解
# # print("\n=== 特征值验证 ===")
# # for i in range(len(eigenvalues)):
# #     λ = eigenvalues[i]
# #     v = eigenvectors[:, i]
# #     print(f"特征值 {λ}: A×v = {A@v}, λ×v = {λ*v}")
#
# # # 均匀分布 [0,1)
# # uniform = np.random.rand(3, 3)            # 3x3均匀分布
# #
# # # 标准正态分布
# # normal = np.random.randn(3, 3)            # 3x3标准正态分布
# #
# # # 指定范围的随机整数
# # randint = np.random.randint(0, 100, (2, 4)) # 2x4，范围[0,100)
# #
# # # 随机选择
# # choice = np.random.choice([1, 2, 3, 4], size=5) # 从列表中随机选择5次
# #
# # print("\n随机数生成:")
# # print("标准正态分布:\n", normal)
# # print("随机整数:\n", randint)
#
# # # arange: 类似range，生成等差数列
# # arr_arange = np.arange(0, 10, 2)          # [0, 2, 4, 6, 8]
# #
# # # linspace: 生成指定数量的等间距数值
# # arr_linspace = np.linspace(0, 1, 5)       # [0., 0.25, 0.5, 0.75, 1.]
# #
# # # logspace: 生成对数等间距数值
# # arr_logspace = np.logspace(0, 2, 5)       # [1., 3.16, 10., 31.62, 100.]
# #
# # print("\n数值序列生成:")
# # print("arange:", arr_arange)
# # print("linspace:", arr_linspace)
# # print("logspace:", arr_logspace)
#
# # # 创建全零数组
# # zeros_1d = np.zeros(5)                    # 一维，5个元素
# # zeros_2d = np.zeros((2, 3))               # 二维，2行3列
# #
# # # 创建全一数组
# # ones_1d = np.ones(4)                      # 一维，4个元素
# # ones_2d = np.ones((3, 2))                 # 二维，3行2列
# #
# # # 创建未初始化数组（内容随机）
# # empty_arr = np.empty((2, 2))              # 快速创建，不初始化
# #
# # # 创建单位矩阵
# # identity = np.eye(3)                      # 3x3单位矩阵
# #
# # print("\n特殊值初始化:")
# # print("zeros_2d:\n", zeros_2d)
# # print("ones_2d:\n", ones_2d)
#
# # import numpy as np
# #
# # # 从Python列表/元组创建
# # arr1 = np.array([1, 2, 3])                    # 一维数组
# # arr2 = np.array([[1, 2, 3], [4, 5, 6]])       # 二维数组
# # arr3 = np.array([1, 2, 3], dtype=float)       # 指定数据类型
# #
# # print("常规创建:")
# # print(arr1)      # [1 2 3]
# # print(arr2)      # [[1 2 3] [4 5 6]]
# # print(arr3)      # [1. 2. 3.] - 浮点数
#
# # import numpy as np
# # a = np.array([[1, 2, 3]])# shape (1,3)
# # b = np.array([4, 5, 6])# shape (3,)
# # result = a + b
# # print(result)